package com.cdy.jxc_project.controller;

import com.cdy.jxc_project.domain.TCustomer;
import com.cdy.jxc_project.domain.TGoods;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TGoodsService;
import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/23 10:42
 * */
@RestController
@RequestMapping("/tGoods")
public class TGoodsController {

    @Resource
    private TGoodsService tGoodsService;
    @AddSysLog
    @PostMapping("/queryTGoodsList/{pId}")
    public R queryTGoodsList(@PathVariable("pId") Integer id){
        List<TGoods> tGoods = tGoodsService.queryTGoodsList(id);
        return  R.ok().message("查询成功").data("goodsList",tGoods);
    }
    @AddSysLog
    @DeleteMapping("/deleteTGoods/{id}")
    public R deleteTGoods(@PathVariable("id") Integer id){
        if (tGoodsService.deleteTGoods(id)>0){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("错误");
        }
    }
    @AddSysLog
    @PostMapping("/insertAndUpdateTGoods")
    public R insertAndUpdateTGoods(@RequestBody TGoods tGoods){
        try{
            if (tGoods.getId()==null){
                tGoodsService.insertTGoods(tGoods);
                return R.ok().message("保存成功");
            }else {
                tGoodsService.updateTGoods(tGoods);
                return R.ok().message("修改成功");
            }

        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }
}
